package tools

import (
	"fmt"
	"log"

	"github.com/saint-yellow/go-pl/toys/sensor-simulator/arguments"
	"github.com/streadway/amqp"
)

const SensorListQueue = "SensorList"

func CreateConnection(url string) *amqp.Connection {
	conn, err := amqp.Dial(url)
	failOnError(err, "Failed to connect to MQ")
	return conn
}

func CreateChannel(conn *amqp.Connection) *amqp.Channel {
	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	return ch
}

func CreateQueue(ch *amqp.Channel) *amqp.Queue {
	q, err := ch.QueueDeclare(*arguments.Name, false, false, false, false, nil)
	failOnError(err, "Failed to declare a queue")
	return &q
}

func failOnError(err error, message string) {
	if err != nil {
		log.Fatalf("%s: %s", err, message)
		panic(fmt.Sprintf("%s: %s", err, message))
	}
}
